package com.gking.centerSystem.controller.publicApi;


import com.gking.centerSystem.AOP.annotationUtil.configFunctionSwitch.ConfigFunctionSwitch;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.User;
import com.gking.centerSystem.service.ConfigSwitch.ConfigSwitchService;
import com.gking.centerSystem.service.SystemInfo.SystemInfoService;
import com.gking.centerSystem.service.User.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api")
public class LoginController {


    @Resource
    private UserService userService;

    @Resource
    private ConfigSwitchService configSwitchService;


    /**
     * 用户注册
     *
     * @return s
     */
    @ConfigFunctionSwitch(code = "OPEN_REGISTER")
    @PostMapping("/register")
    public Result<String> regUser(@RequestBody User user) {
//        String configCode = ConfigSwitchCodeEnum.OPEN_REGISTER.getCode();
//        Result<Boolean> booleanResult = configSwitchService.judgeConfigSwitchStatus(configCode);
//        if (!booleanResult.getData()) {
//            return Result.error(500, booleanResult.getMessage());
//        }

        log.warn("注册信息为：{}", user);
        String account = user.getAccount();
        String password = user.getPassword();
        if (account.isEmpty() || password.isEmpty()) return Result.error("请将参数补充完整！");

        return userService.regUser(user.getAccount(), user.getPassword());
    }


    /**
     * 验证码
     *
     * @param request 线程
     * @return s
     */
    @ConfigFunctionSwitch(code = "OPEN_LOGIN")
    @PostMapping("/verificationCode")
    public Result<String> verificationCode(HttpServletRequest request) {
        return userService.verificationCode(request);
    }


    /**
     * 用户登录
     *
     * @param request 线程
     * @param user    用户信息
     * @param code    验证码
     * @return s
     */
    @ConfigFunctionSwitch(code = "OPEN_LOGIN")
    @PostMapping("/login")
    public Result<String> login(HttpServletRequest request, @RequestBody User user, @RequestParam String code) {
//        String configCode = ConfigSwitchCodeEnum.OPEN_LOGIN.getCode();
//        Result<Boolean> booleanResult = configSwitchService.judgeConfigSwitchStatus(configCode);
//        if (!booleanResult.getData()) {
//            return Result.error(500, booleanResult.getMessage());
//        }

        log.warn("登录信息为：{}", user);
        String account = user.getAccount();
        String password = user.getPassword();
        if (account.isEmpty() || password.isEmpty() || code.isEmpty())
            return Result.error("请将参数补充完整！");

        return userService.login(request, account, password, code);
    }


    /**
     * 判断某个系统配置开关，配置功能是否启用了
     *
     * @param configCode 配置代码
     * @return b
     */
    @PostMapping("/judgeConfigSwitchStatus")
    public Result<Boolean> judgeConfigSwitchStatus(@RequestParam String configCode) {
        return configSwitchService.judgeConfigSwitchStatus(configCode);
    }

}
